<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title>three.js - webgl cube reflection / refraction [Walt]</title>
		<meta charset="utf-8">
		<style type="text/css">
			body {
				background:#000;
				color:#fff;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
			}
			a {	color: #ff0080;	text-decoration: none; }
			a:hover { color: #0080ff; }

			canvas { pointer-events:none; z-index:10; position:relative; }
			#log { position:absolute; top:50px; text-align:left; display:block; z-index:100; pointer-events:none; }
			#d { text-align:center; margin:1em 0 -7.5em 0; z-index:1000; position:relative; display:block }
			.button { background:orange; color:#fff; padding:0.2em 0.5em; cursor:pointer }
			.inactive { background:#999; color:#eee }
		</style>
	</head>

	<body>
		<div id="d">
			<p><a href="http://github.com/mrdoob/three.js">Three.js</a> cube mapping demo

			<p>Walt Disney head by <a href="http://www.davidoreilly.com/2009/01/walt-disneys-head-on-a-plate" target="_blank">David OReilly</a>
			<p>Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank">Humus</a>
		</div>

		<pre id="log"></pre>

		<script type="text/javascript" src="../build/ThreeExtras.js"></script> 

		<script type="text/javascript" src="js/Stats.js"></script>

		<script type="text/javascript">

			var container, stats;

			var camera, scene, webglRenderer;
			var cameraCube, sceneCube;

			var mesh, zmesh, lightMesh, geometry;

			var loader;
			
			var directionalLight, pointLight;

			var mouseX = 0;
			var mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			document.addEventListener('mousemove', onDocumentMouseMove, false);

			init();
			setInterval( loop, 1000 / 60 );

			function init() {

				container = document.createElement('div');
				document.body.appendChild(container);

				camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 100000 );
				camera.position.z = 2000;

				cameraCube = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 100000 );

				scene = new THREE.Scene();
				sceneCube = new THREE.Scene();

				// LIGHTS

				var ambient = new THREE.AmbientLight( 0xffffff );
				scene.addLight( ambient );

				pointLight = new THREE.PointLight( 0xffffff, 2 );
				scene.addLight( pointLight );

				// light representation
				sphere = new Sphere( 100, 16, 8 );
				lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color:0xffaa00 } ) );
				lightMesh.position = pointLight.position;
				lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
				scene.addObject(lightMesh);

				var path = "textures/cube/SwedishRoyalCastle/";
				var format = '.jpg';
				var urls = [
						path + 'px' + format, path + 'nx' + format,
						path + 'py' + format, path + 'ny' + format,
						path + 'pz' + format, path + 'nz' + format
					];

				var images = ImageUtils.loadArray( urls );

				var reflectionCube = new THREE.Texture( images );
				var refractionCube = new THREE.Texture( images, new THREE.CubeRefractionMapping() );
				
				//var cubeMaterial3 = new THREE.MeshPhongMaterial( { color: 0x000000, specular:0xaa0000, env_map: new THREE.TextureCube( images ), combine: THREE.MixOperation, reflectivity: 0.25 } );
				var cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xff6600, env_map: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
				var cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xffee00, env_map: refractionCube, refraction_ratio: 0.95 } );
				var cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, env_map: reflectionCube } )

				//SceneUtils.addPanoramaCubePlanes( sceneCube, 100000, images );
				//SceneUtils.addPanoramaCube( sceneCube, 100000, images );
				SceneUtils.addPanoramaCubeWebGL( sceneCube, 100000, reflectionCube );
				
				webglRenderer = new THREE.WebGLRenderer();
				webglRenderer.setSize( window.innerWidth, window.innerHeight );
				webglRenderer.autoClear = false;
				//webglRenderer.setFaceCulling( 0 );
				container.appendChild( webglRenderer.domElement );

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				stats.domElement.style.zIndex = 100;
				container.appendChild( stats.domElement );

				loader = new THREE.Loader( true );
				document.body.appendChild( loader.statusDomElement );
				
				loader.loadBinary( "obj/walt/WaltHead_bin.js", function( geometry ) { createScene( geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3 ) }, "obj/walt" );

			}

			function createScene( geometry, m1, m2, m3 ) {

				var s = 15;

				SceneUtils.addMesh( scene, geometry, s,      0, 0, -100, 0,0,0, m1 );
				SceneUtils.addMesh( scene, geometry, s,   -900, 0, -100, 0,0,0, m2 );
				SceneUtils.addMesh( scene, geometry, s,    900, 0, -100, 0,0,0, m3 );
				
				loader.statusDomElement.style.display = "none";

			}

			function onDocumentMouseMove(event) {

				mouseX = ( event.clientX - windowHalfX ) * 4;
				mouseY = ( event.clientY - windowHalfY ) * 4;

			}

			var r = 0;

			function loop() {

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				cameraCube.target.position.x = - camera.position.x;
				cameraCube.target.position.y = - camera.position.y;
				cameraCube.target.position.z = - camera.position.z;


				lightMesh.position.x = 1500 * Math.cos( r );
				lightMesh.position.z = 1500 * Math.sin( r );

				r += 0.01;

				webglRenderer.clear();
				webglRenderer.render( sceneCube, cameraCube );
				webglRenderer.render( scene, camera );

				stats.update();

			}

			function log(text) {

				var e = document.getElementById("log");
				e.innerHTML = text + "<br/>" + e.innerHTML;

			}
		</script>

	</body>
</html>
